package com.lanqiao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyUtil {

    public static void main(String[] args) throws IOException {
        Detect detect = new Detect();
        Thread thread = new Thread(detect);
        thread.start();
        try {

            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String sourcePath = Detect.str[0];
        String path = "D:\\U盘文件";

        copyDir(sourcePath, path);
    }


    public static void copyDir(String sourcePath, String path) throws IOException {
        File file = new File(sourcePath);
        String[] filePath = file.list();

        if (!new File(path).exists()) {
            new File(path).mkdir();
        }

        for (int i = 0; i < filePath.length; i++) {
            //判断当前循环到的东西是不是一个文件夹
            File f = new File(sourcePath + file.separator + filePath[i]);
            if (f.isDirectory()) {
                copyDir(sourcePath  + file.separator  + filePath[i], path  + file.separator + filePath[i]);
            }
            //判断当前循环到的东西是不是一个文件
            if (f.isFile()) {
                copyFile(sourcePath + file.separator + filePath[i], path + file.separator + filePath[i]);
            }
        }
    }

    public static void copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);;

        byte[] buffer=new byte[1024];
        int readByte = 0;
        while((readByte = in.read(buffer)) != -1){
            out.write(buffer, 0, readByte);
            out.flush();
        }
        in.close();
        out.close();
    }
}
